// V-backend/src/routes/admin.js
const express = require('express');
const router = express.Router();
const { adminAuth } = require('../middleware/auth');  // 已修复
const adminController = require('../controllers/adminController');
const analyticsController = require('../controllers/analyticsController');
const fansController = require('../controllers/fansController');
const videoController = require('../controllers/videoController');

// 所有管理后台路由都需要管理员认证
router.use(adminAuth);  // 使用 adminAuth 而不是 auth

// 仪表板数据 - 修改路径为 /dashboard
router.get('/dashboard', adminController.getDashboardStats);

// 用户管理
router.get('/users', adminController.getUsers);
router.get('/anchors', adminController.getAnchors);

// 活动管理
router.get('/activities', adminController.getActivities);
router.post('/activities', adminController.createActivity);
router.get('/activities/:id', adminController.getActivityById); // 获取活动详情
router.put('/activities/:id', adminController.updateActivity); // 更新活动
router.delete('/activities/:id', adminController.deleteActivity); // 删除活动
router.put('/activities/:id/status', adminController.updateActivityStatus);

// 商品管理
router.get('/products', adminController.getProducts);
router.post('/products', adminController.createProduct); // 创建商品
router.get('/products/:id', adminController.getProductById); // 获取商品详情
router.put('/products/:id', adminController.updateProduct); // 更新商品
router.delete('/products/:id', adminController.deleteProduct); // 删除商品
router.put('/products/:id/status', adminController.updateProductStatus);

// 商品图片管理 - 新增路由
router.post('/products/:id/images', adminController.uploadProductImages); // 上传图片
router.put('/products/:id/images/sort', adminController.sortProductImages); // 图片排序
router.delete('/products/images/:imageId', adminController.deleteProductImage); // 删除图片
router.put('/products/images/:imageId/cover', adminController.setProductCover); // 设置封面

// 订单管理 - 修复重复路由问题
router.get('/orders', adminController.getOrders);
router.get('/orders/:id', adminController.getOrderById); // 订单详情
router.get('/orders-stats', adminController.getOrderStats); // 修改路径避免冲突
router.put('/orders/:id/status', adminController.updateOrderStatus);
router.put('/orders/bulk-status', adminController.bulkUpdateOrderStatus); // 批量更新状态

// 数据分析
router.get('/analytics/sales', analyticsController.getSalesAnalytics);
router.get('/analytics/users', analyticsController.getUserAnalytics);
router.get('/analytics/activities', analyticsController.getActivityAnalytics);
router.get('/analytics/overview', analyticsController.getOverviewAnalytics);

// 视频管理
router.get('/videos', videoController.getVideos);
router.post('/videos', videoController.createVideo);
router.get('/videos/:id', videoController.getVideoById);
router.put('/videos/:id', videoController.updateVideo);
router.delete('/videos/:id', videoController.deleteVideo);
router.put('/videos/:id/status', videoController.updateVideoStatus);
router.post('/videos/:id/upload', videoController.uploadVideo, videoController.uploadVideoFile);
router.post('/videos/:id/cover', videoController.uploadCover, videoController.uploadVideoCover);
router.get('/videos-stats', videoController.getVideoStats);

// 粉丝管理
router.get('/fans/list', fansController.getFansList);
router.get('/fans/stats', fansController.getFansStats);
router.get('/fans/export', fansController.exportFansData);
router.get('/fans/:id', fansController.getFanDetail); // 这个要放在最后

// 新增：获取管理员个人信息
router.get('/profile', adminController.getAdminProfile);

module.exports = router;